<template>
  <view class="container">
    <z-paging ref="paging" v-model="list" @query="getlist">
      <template #top>
        <view class="header">
          <text class="header-title">训练计划</text>
        </view>
      </template>

      <view class="plan-list">
        <view class="plan-card" v-for="(item, index) in list" :key="index">
          <view class="plan-info" @click="handleTargetDetail(item)">
            <text class="plan-name">{{ item.planName }}</text>
            <text class="plan-date">
              {{ item.planDate ? item.planDate.split(" ")[0] : "" }}
            </text>
          </view>
          <view class="card-actions">
            <view
              class="delete-button"
              @click.stop="confirmDeletePlan(item.id)"
            >
              <image
                src="../../static/del.png"
                style="width: 33rpx; height: 33rpx"
              ></image>
            </view>
            <view class="card-indicator">
              <u-icon name="arrow-right" color="#999" size="28"></u-icon>
            </view>
          </view>
        </view>
      </view>

      <template #bottom>
        <view class="add-button" @click="addPlan">
          <text class="button-text">添加计划</text>
        </view>
      </template>
    </z-paging>
  </view>
</template>

<script>
import { planIns, deletePlan } from "../../api/fit";
export default {
  data() {
    return {
      list: [],
      id: "",
      name: "",
    };
  },
  onShow() {
    this.$refs.paging.reload();
  },
  onLoad(e) {
    this.id = e.id;
    console.log(e, "eeeeee");
    this.name = e.name;
  },
  methods: {
    async getlist(pageNo, pageNum = 10) {
      let data = {
        page: pageNo,
        size: pageNum,
        planName: "",
        customerId: this.id,
      };
      const res = await planIns(data);
      this.$refs.paging.complete(res.data.list);
    },
    addPlan() {
      uni.navigateTo({
        url: `/pages/customer/addplan?id=${this.id}`,
      });
    },
    handleTargetDetail(val) {
      uni.navigateTo({
        url: `/pages/customer/plandetail?id=${val.id}&name=${this.name}&customerId=${this.id}`,
      });
    },
    confirmDeletePlan(id) {
      uni.showModal({
        title: "确认删除",
        content: "您确定要删除这个计划吗？",
        success: (res) => {
          if (res.confirm) {
            this.deletePlan(id);
          }
        },
      });
    },
    async deletePlan(id) {
      let arr = [id];
      const res = await deletePlan(arr);
      if (res.code == 200) {
        uni.showToast({
          title: "删除成功",
          icon: "success",
        });
        this.getlist(1);
      }
      // 删除计划的逻辑
      console.log(`删除计划ID: ${id}`);
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background-color: #f7f8fa;
  padding-bottom: 120rpx;
}

.header {
  background: #fff;
  padding: 40rpx 30rpx;
  box-shadow: 0 2rpx 20rpx rgba(0, 0, 0, 0.03);
  position: sticky;
  top: 0;
  z-index: 10;

  &-title {
    font-size: 36rpx;
    font-weight: 700;
    color: #333;
    text-align: center;
    display: block;
    letter-spacing: 2rpx;
  }
}

.plan-list {
  padding: 30rpx;
}

.plan-card {
  background: #fff;
  border-radius: 20rpx;
  margin-bottom: 24rpx;
  padding: 40rpx 30rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;

  &:active {
    transform: scale(0.98);
    background: #fafafa;
  }
}

.plan-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  cursor: pointer;
}

.plan-name {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
}

.plan-date {
  font-size: 28rpx;
  color: #999;
  margin-top: 10rpx;
}

.card-actions {
  display: flex;
  align-items: center;
}

.delete-button {
  margin-right: 20rpx;
  cursor: pointer;
  transition: transform 0.3s ease;

  &:hover {
    transform: scale(1.1);
  }
}

.card-indicator {
  opacity: 0.6;
}

.add-button {
  margin: 20rpx auto;
  width: 90%;
  background: #000;
  height: 96rpx;
  border-radius: 48rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8rpx 24rpx rgba(74, 144, 226, 0.3);
  transition: all 0.3s ease;
  &:active {
    transform: translateX(-50%) scale(0.98);
    box-shadow: 0 4rpx 12rpx rgba(74, 144, 226, 0.2);
  }

  .button-text {
    color: #fff;
    font-size: 32rpx;
    font-weight: 600;
    letter-spacing: 4rpx;
  }
}

::-webkit-scrollbar {
  width: 0;
  background: transparent;
}
</style>
